//
//  YYRunLoopObserver.m
//  YYRunLoop
//
//  Created by SMCB on 2017/11/1.
//  Copyright © 2017年 SMCB. All rights reserved.
//

#import "YYRunLoopObserver.h"

#import "YYRunLoopService.h"

@implementation YYRunLoopObserver {
    CFRunLoopObserverRef _observerRef;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addobserver];
    }
    return self;
}

- (void)addobserver {
    _observerRef = CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopAllActivities, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        switch (activity) {
            case kCFRunLoopEntry:
                NSLog(@"Entry");
                break;
            case kCFRunLoopBeforeTimers:
                NSLog(@"BeforeTimers");
                break;
            case 1UL << 2:
                NSLog(@"BeforeSources");
                break;
            case 1UL << 3:
                NSLog(@"1UL << 3");
                break;
            case 1UL << 4:
                NSLog(@"1UL << 4");
                break;
            case kCFRunLoopBeforeWaiting:
                NSLog(@"BeforeWaiting");
                break;
            case kCFRunLoopAfterWaiting:
                NSLog(@"AfterWaiting");
                break;
            case kCFRunLoopExit:
                NSLog(@"Exit");
                break;
            default:
                break;
        }
    });
    
    CFRunLoopAddObserver(CFRunLoopGetMain(), _observerRef, kCFRunLoopDefaultMode);
}

- (void)dealloc {
    CFRunLoopRemoveObserver(CFRunLoopGetMain(), _observerRef, kCFRunLoopDefaultMode);
}
@end
